import LocaleUtil from "./LocaleUtil";
export default {
  getColumns(record, dictMap, ajaxGetDictItems, ajaxGetSqlConfigItems, ajaxGetHttpConfigItems, form, reloadData) {
    const columns = []
    record.fields.forEach(field => {
      const column = Object.assign({}, field)
      column.disabled = field.disabled === 'Y'
      if (typeof column.title !== "undefined") {
        column.title = LocaleUtil.getLocale(column.title)
      }
      if (column.required === 'Y') {
        column.validateRules = [{required: true, message: column.title + LocaleUtil.getLocale('m.common.tip.cannotNull')}]
      }
      if (column.showConfig && column.showConfig !== '') {
        column.options = []
        const showConfigJson = JSON.parse(column.showConfig)
        if (showConfigJson.options) {
          const options = showConfigJson.options
          column.options.push(...options)
          this.formatOptionsByExpr(showConfigJson.formatExpr, column.options)
        } else if (showConfigJson.dictOption && showConfigJson.dictOption.dictCode) {
          if (dictMap && dictMap.hasOwnProperty(showConfigJson.dictOption.dictCode)) {
            const options = dictMap[showConfigJson.dictOption.dictCode]
            column.options.push(...options)
            this.formatOptionsByExpr(showConfigJson.formatExpr, column.options)
          } else {
            ajaxGetDictItems(showConfigJson.dictOption.dictCode, null).then((res) => {
              if (res.success) {
                const options = res.result
                column.options.push(...options)
                this.formatOptionsByExpr(showConfigJson.formatExpr, column.options)
                reloadData()
                if (dictMap) {
                  // 不起作用了，不如注释掉
                  // dictMap[showConfigJson.dictOption.dictCode] = res.result
                }
              }
            })
          }
        } else if (showConfigJson.sqlConfig && showConfigJson.sqlConfig.sqlCode) {
          if (showConfigJson.linkage) {
            column.options = []
            column.linkage = showConfigJson.linkage
          } else {

            const parameter = {}
            // 入参
            if (showConfigJson.sqlConfig.sqlParameterConfig && showConfigJson.sqlConfig.sqlParameterConfig.length > 0) {
              showConfigJson.sqlConfig.sqlParameterConfig.forEach(item => {
                let value = null;
                if (item.outKey && form) {
                  value = form.getFieldValue(item.outKey)
                }
                if (!value && item.defaultValue) {
                  value = item.defaultValue
                }
                if (value) {
                  parameter[item.parameterKey] = value
                }
              })
            }

            ajaxGetSqlConfigItems(showConfigJson.sqlConfig.sqlCode, parameter).then((res) => {
              if (res.success) {
                const options = res.result
                column.options.push(...options)
                this.formatOptionsByExpr(showConfigJson.formatExpr, column.options)
                reloadData()
              }
            })
          }
        } else if (showConfigJson.httpConfig && showConfigJson.httpConfig.httpCode) {
          if (showConfigJson.linkage) {
            column.options = []
            column.linkage = showConfigJson.linkage
          } else {
            ajaxGetHttpConfigItems(showConfigJson.httpConfig.httpCode, {httpConfig: showConfigJson.httpConfig}).then((res) => {
              if (res.success) {
                const options = res.result
                column.options.push(...options)
                this.formatOptionsByExpr(showConfigJson.formatExpr, column.options)
                reloadData()
              }
            })
          }
        } else {
          column.options = []
        }
      }
      columns.push(column)
    })
    return columns
  },
  formatOptionsByExpr(formatExpr, options) {
    options.forEach(item => {
      let value = item.text || item.label || item.title
      let key = item.value
      let text = "";
      if (formatExpr && value) {
        text = formatExpr.replace(/\$\{key\}/g, !key ? "" : key).replace(/\$\{value\}/g, !value ? "" : value);
      } else {
        text = value
      }
      item.text = text
      item.label = text
      item.title = text
    })
  },
  getRules(record, validRule, validatorPk, validatorUnionCheck) {
    const result = []
    if (record.rules) {
      record.rules.forEach(vo => {
        const newRule = Object.assign({}, vo)
        if(typeof newRule.message !== "undefined"){
          newRule.message = LocaleUtil.getLocale(newRule.message)
        }
        if (vo.pattern) {
          const rule = validRule.find(rule => rule.value === vo.pattern)
          if (rule) {
            newRule.pattern = rule.pattern
            newRule.message = LocaleUtil.getLocale(rule.message)
            newRule.transform = (value) => {
              if (typeof value === undefined || value === null) {
                return value;
              }
              if (isNaN(value)) {
                return value;
              } else {
                return value.toString();
              }
            }
          }
        } else if (vo.pk) {
          newRule.validator = validatorPk
        } else if (vo.unionCheck) {
          newRule.validator = validatorUnionCheck
        }
        if (record.type === 'input' && newRule.required) {
          newRule.whitespace = true
        }
        if (record.type === 'input' && (newRule.max || newRule.required)) {
          newRule.transform = (value) => {
            return typeof value === 'number' ? value.toString() : value
          }
        }
        result.push(newRule)
      })
    }
    return result
  },
  textStyleDiv(record) {
    const styleObj = {}
    if (record.options.textAlign) {
      styleObj.textAlign = record.options.textAlign
    }
    if (record.options.background) {
      styleObj.background = record.options.background
    }
    return styleObj
  },
  textStyleLabel(record) {
    const styleObj = {}
    if (record.options.fontSize) {
      styleObj.fontSize = record.options.fontSize + 'px'
    }
    if (record.options.fontWeight) {
      styleObj.fontWeight = record.options.fontWeight
    }
    if (record.options.color) {
      styleObj.color = record.options.color
    }
    return styleObj
  },
  findItemByKey(jsonList, key) {
    if (!jsonList || !key) {
      return
    }
    for (let i = 0; i < jsonList.length; i++) {
      const item = jsonList[i]
      if (item.key === key) {
        return item;
      }
      const listItem = this.findItemByKey(item.list, key)
      if (listItem) {
        return listItem
      }
      const trsItem = this.findItemByKey(item.trs, key)
      if (trsItem) {
        return trsItem
      }
      const tdsItem = this.findItemByKey(item.tds, key)
      if (tdsItem) {
        return tdsItem
      }
      const columnsItem = this.findItemByKey(item.columns, key)
      if (columnsItem) {
        return columnsItem
      }
      const tabsItem = this.findItemByKey(item.tabs, key)
      if (tabsItem) {
        return tabsItem
      }
    }
  },
}
